package com.apes.fn.scm.settle.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

/**
 * @Title: PlatformSettleDao
 * @Author fj
 * @Package com.apes.fn.scm.settle.service
 * @Date 2023/10/9 14:23
 * @description:
 */
@Component
public class PlatformSettleDao extends ApesDao {


    public JSONArray taxBillQuery(JSONObject param) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT A.ID settle_id,B.ID platform_settle_id,A.receipt_state,B.SUPPLIER_ID,A.COMPANY_ID,TO_CHAR(B.reconcile_begin_date,'yyyy-MM-dd') reconcile_begin_date,\n" +
                "         TO_CHAR(B.reconcile_end_date,'yyyy-MM-dd') reconcile_end_date,B.loan_amt,B.payable_amt,TO_CHAR(B.create_date,'yyyy-MM-dd HH24:mi:ss') create_date,A.invoiced_amount, B.payment_month,\n" +
                "(SELECT C.NAME FROM MAS_PARTY C, MAS_PARTY_ROLE D WHERE C.ID = D.PARTY_ID AND D.ID = B.SUPPLIER_ID) SUPPLIER,\n" +
                "(SELECT C.NAME FROM MAS_PARTY C, MAS_PARTY_ROLE D WHERE C.ID = D.PARTY_ID AND D.ID = A.COMPANY_ID) COMPANY "+
                "FROM ACC_SETTLE A,PLATFORM_SETTLE B\n" +
                "WHERE A.ORIGIN = B.ID AND A.RECEIPT_STATE != 'already'");
        conditionSplicing(param,sb);
        return toJSONArray(find(sb.toString(),param));
    }

    private void conditionSplicing(JSONObject param,StringBuilder sql){
        if (param.containsKey("platformSettleId"))                sql.append(" AND B.ID = :platformSettleId");
        if (param.containsKey("supplierId"))   sql.append(" AND B.SUPPLIER_ID = :supplierId");
        if (param.containsKey("settleId"))                sql.append(" AND A.ID = :settleId");
        if (param.containsKey("companyId"))                sql.append(" AND A.COMPANY_ID = :companyId");
        if (param.containsKey("reconcileBeginDate"))        sql.append(" AND B.reconcile_begin_date >= to_date(:reconcileBeginDateGTE,'yyyy-MM-dd HH24:mi:ss') AND B.reconcile_begin_date <= to_date(:reconcileBeginDateLTE,'yyyy-MM-dd HH24:mi:ss')");
        if (param.containsKey("reconcileEndDate"))       sql.append(" AND B.reconcile_end_date >= to_date(:reconcileEndDateGTE,'yyyy-MM-dd HH24:mi:ss') AND B.reconcile_end_date <= to_date(:reconcileEndDateLTE,'yyyy-MM-dd HH24:mi:ss')");
    }

    public JSONArray taxBillQueryCount(JSONObject param) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT count(1) count FROM ACC_SETTLE A,PLATFORM_SETTLE B WHERE A.ORIGIN = B.ID AND A.RECEIPT_STATE != 'already'");
        conditionSplicing(param,sb);
        return toJSONArray(find(sb.toString(),param));
    }
}
